package com.aaa.hospital.ManagementEnd.controller;

import com.aaa.hospital.ManagementEnd.pojo.Dispensary;
import com.aaa.hospital.ManagementEnd.pojo.Docuser;
import com.aaa.hospital.ManagementEnd.pojo.Medicine;
import com.aaa.hospital.ManagementEnd.pojo.Unit;
import com.aaa.hospital.ManagementEnd.service.Impl.DispensarySerImpl;
import com.aaa.hospital.ManagementEnd.service.Impl.MedicinImpl;
import com.aaa.hospital.ManagementEnd.service.Impl.UnitSerImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//药品页面
@Controller
@RequestMapping("/medicine")
public class MedicineCon {
    @Autowired
    private MedicinImpl medicinImpl;
    @Autowired
    private UnitSerImpl unitSerImpl;
    @Autowired
    private DispensarySerImpl dispensarySerImpl;
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    // 将java对象转为json格式的数据
    @ResponseBody
    //查询信息
    public String selectAll(){
        JSONObject json = new JSONObject();
        List<Medicine> data = medicinImpl.queryAll();
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }
    //修改
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> update(Medicine medicine) {
        int result = medicinImpl.update(medicine);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //修改库存
    @RequestMapping(value = "/updateCK",method = RequestMethod.GET)
    @ResponseBody
    private String updateCK(Integer ypid,Integer jnum) {
        medicinImpl.updateKC(ypid,jnum);
        return JSON.toJSONString(true);
    }

    //搜索框查询
    @RequestMapping(name="搜索信息",value = "/select_ypId_ypName",method = RequestMethod.GET)
    @ResponseBody
    public String selectName(@Param("ypid") Integer ypid,@Param("ypname") String ypname){
        JSONObject json = new JSONObject();
        List<Docuser> data =medicinImpl.select_ypId_ypName(ypid,ypname);
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }
    //删除
    @RequestMapping(value = "/{ypid}", method = RequestMethod.GET)
    @ResponseBody
    //@PathVariable注解绑定它传过来的值到方法的参数上。
    private Map<String, Object> delete(@PathVariable("ypid") Integer ypid) {
        int result = medicinImpl.delete(ypid);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //添加
    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> insert(Medicine medicine) {
        int result =medicinImpl.insert(medicine);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //查询药品单位
    @RequestMapping(value = "/SelectUnit",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String SelectUnit(){
        List<Unit> list = unitSerImpl.queryAll();
        return JSON.toJSONString(list);

    }


    //查询药房
    @RequestMapping(value = "/SelectHome",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String SelectHome(){
        List<Dispensary> list = dispensarySerImpl.queryAll();
        return JSON.toJSONString(list);
    }

}
